package com.mdd.front.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.front.service.product.IProductPropertyFrontService;
import com.mdd.front.service.product.IProductPropertyValueFrontService;
import com.mdd.front.vo.property.ProductPropertyValueDetailVo;
import com.mdd.front.vo.spu.ProductPropertyDetailVo;
import com.mdd.product.entity.ProductProperty;
import com.mdd.product.mapper.ProductPropertyMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 商品属性项实现类
 * @author LikeAdmin
 */
@Service
public class ProductPropertyFrontServiceImpl implements IProductPropertyFrontService {

    @Resource
    ProductPropertyMapper productPropertyMapper;

    @Resource
    IProductPropertyValueFrontService iProductPropertyValueFrontService;

    /**
     * 商品属性项列表
     *
     * @author LikeAdmin
     * @return PageResult<OmsProductPropertyListedVo>
     */
    @Override
    public List<ProductPropertyDetailVo> list(Long spuId) {
        LambdaQueryWrapper<ProductProperty> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductProperty::getSpuId,spuId);
        List<ProductProperty> data = productPropertyMapper.selectList(queryWrapper);

        List<ProductPropertyDetailVo> list = new LinkedList<>();
        for(ProductProperty item : data) {
            ProductPropertyDetailVo vo = new ProductPropertyDetailVo();
            BeanUtils.copyProperties(item, vo);

            List<ProductPropertyValueDetailVo> productPropertyValueDetailVos = iProductPropertyValueFrontService.list(vo.getId());
            vo.setPropertyValues(productPropertyValueDetailVos);
            list.add(vo);
        }

        return list;
    }

    /**
     * 商品属性项详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OmsProductProperty
     */
    @Override
    public ProductPropertyDetailVo detail(Long id) {
        ProductProperty model = productPropertyMapper.selectById(id);

        Assert.notNull(model, "数据不存在");

        ProductPropertyDetailVo vo = new ProductPropertyDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }
}
